<?php
namespace app\api\controller\more;

use app\common\controller\Api;
use think\Db;
/**
 * 示例接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    /**
     * 报价单详情
     *  cmf_quotaion_log
     *  quotation_id
     */
    public function getquotaionbyquotationid()
    {
        $quotaionid=$this->request->param('quotation_id',0,'intval');

        $lists=Db::name('quotaion_log')
            ->alias('a')
            ->join('catefrolist b','a.catefrolist_id=b.id')
            ->field('a.*,b.name,b.images as iconimg,b.name')
            ->where('quotation_id','eq',$quotaionid)
            ->select();


        foreach ($lists as $k=>&$v){
            $v['images']=getallimages($v['images']);
            $v['iconimg']=getallimages1($v['iconimg']);


        }

        $this->success('返回成功',$lists);

    }


    /**
     * 获取我的历史报价单
     */
    public function getmyhistoryquoted(){


        $id=$this->request->param('id',0,'intval');


//        $lists=Db::name('quotaion_log')->alias('a')
//            ->join('cmf_quotation b','a.quotation_id=b.id')
//            ->join('cmf_aa_merch c','b.merch_id=c.id')
//            ->where('a.id','eq',$id)
//            ->field('a.*,b.addtime,c.title')
//            ->limit(5)
//            ->select();

        $find=Db::name('quotaion_log')->where('id','eq',$id)
            ->find();



        $lists=Db::name('catefrolist_history')->where('catefrolist_id','eq',$find['catefrolist_id'])
            ->order('addtime','desc')
            ->select();


        foreach ($lists as $k=>&$v){
            $v['addtime']=date('Y-m-d',$v['addtime']);
        }


        $this->success('操作成功',$lists);

    }

    /**
     * 修改价格
     */
    public function setcarmoneybyid(){

        $user=$this->auth->getUserinfo();



        $id=$this->request->param('id',0,'intval');

        $money=$this->request->param('money',0,'intval');
        $desc=$this->request->param('desc',0,'trim');


        $find=Db::name('quotaion_log')->where('id','eq',$id)
            ->find();

        $findb=Db::name('quotation')
            ->where('id','eq',$find['quotation_id'])
            ->find();

        if ($findb['lcok']==1){
            $this->error('已经锁定');
        }



        $res=Db::name('quotaion_log')
            ->where('id','eq',$id)
            ->update(['money'=>$money]);

        if($res){

            $merch=Db::name('aa_merch')->where(['id'=>$findb['merch_id']])->find();

            $arr['merch_id']=$merch['id'];
                $arr['merch_name']=$merch['title'];

            $arr['catefrolist_id']=$find['catefrolist_id'];
            $arr['moneya']=$find['money'];
            $arr['moneyb']=$money;
            $arr['desc']=$desc;
            $arr['user_id']=$this->auth->id;
            $arr['addtime']=time();
            $arr['nickname']=$user['nickname'];

            Db::name('catefrolist_history')->insert($arr);


            //添加一个记录
            $setlog['nickname']=$user['nickname'];
            $setlog['addtime']=time();
            $setlog['desc']='修改金额为:'.$money;
            $setlog['quotation_id']=$find['quotation_id'];

            Db::name('quotation_log_history')->insert($setlog);


            $this->success('修改成功');
        }else   {
            $this->error('修改失败');
        }

    }

    /**
     * 删除信息
     */
    public function deletebycar()
    {

        $id=$this->request->param('id',0,'intval');


        $log=Db::name('quotaion_log')->where('id','eq',$id)
            ->find();

        $find=Db::name('quotation')
            ->where('id','eq',$log['quotation_id'])
            ->find();

        if ($find['lcok']==1){
            $this->error('已经锁定');
        }

        Db::name('quotaion_log')->where('id','eq',$id)
            ->delete();

        //添加一个记录
        $setlog['nickname']=$this->auth->getUser()->nickname;
        $setlog['addtime']=time();
        $setlog['desc']='删除了报价单:'.$log['catefrolist_name'];
        $setlog['quotation_id']=$log['quotation_id'];

        Db::name('quotation_log_history')->insert($setlog);






        $this->success('删除成功');

    }


    /**
     * 单个详情
     */
    public function getgoodforcarbyid(){

        $id=$this->request->param('id',0,'intval');

        $lists=Db::name('quotaion_log')
            ->where('id','eq',$id)
            ->find();

        $lists['images']=getallimages($lists['images']);


        $list=Db::name('desc_icons')->order('weigh desc')->select();

        foreach ($list as $k=>&$v){

            if(str_contains($lists['icons'],$v['name'])){
                $list[$k]['status']=1;
            }
        }


        $this->success('操作成功',['lists'=>$lists,'icon'=>$list]);
    }


    /**
     * 修改备注信息
     * desc,carimg,carid,caricon
     */
    public function setcargooddesc(){

        $id=$this->request->param('carid',0,'intval');
        $desc=$this->request->param('desc',0,'trim');
        $carimg=$this->request->param('carimg',0,'trim');
        $caricon=$this->request->param('caricon',0,'trim');


        $caricon=trim($caricon,',');

        $log=Db::name('quotaion_log')->where('id','eq',$id)
            ->find();


        $ret=Db::name('quotaion_log')
            ->where('id','eq',$id)
            ->update(['desc'=>$desc,'images'=>$carimg,'icons'=>$caricon]);

        if ($ret){

            //添加一个记录
            $setlog['nickname']=$this->auth->getUser()->nickname;
            $setlog['addtime']=time();
            $setlog['desc']='删除了报价单:'.$log['catefrolist_name'];
            $setlog['quotation_id']=$log['quotation_id'];

            Db::name('quotation_log_history')->insert($setlog);




            $this->success('修改成功');
        }

        $this->error('修改失败');


    }




}